﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ConnectionLayer;
using System.Data.SqlClient;

namespace PresentationLayer
{
    public partial class Login : Form
    {
        public Login(ApplicationContext ac)
        {
            this.apc = ac;
            InitializeComponent();
        }

        private void OK_Click(object sender, EventArgs e)
        {
            ConnectionFactory cf = new ConnectionFactory();
            try
            {
                cf.GetConnection.Open();
                SqlCommand cmd = cf.GetConnection.CreateCommand();
                this.textBoxIdFunc.SelectAll();
                cmd.CommandText = "select designacaoProf from Funcionario INNER JOIN Profissao on(profissao=idProf) where Funcionario.codFunc = @codFunc";
                cmd.Parameters.Add(new SqlParameter("@codFunc", this.textBoxIdFunc.SelectedText));

                object o = cmd.ExecuteScalar();
                if (o != null)
                {
                    ApplicationData.Profissao_Utilizador = o.ToString();
                    ApplicationData.IdFuncionario = this.textBoxIdFunc.SelectedText;
                    this.apc.MainForm = new Form1(this.apc);
                    this.apc.MainForm.Show();
                    this.Close();
                }
            }
            catch (Exception ex) { Console.WriteLine(ex.Message); }
            finally
            {
                cf.GetConnection.Close();
            }
        }

        private void Sair_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
